from thrift.transport import TSocket
from thrift.transport.TTransport import TBufferedTransport
from thrift.protocol import TBinaryProtocol

from hbase import Hbase
from hbase.ttypes import *

def create_client(transport):
    protocol = TBinaryProtocol.TBinaryProtocol(transport)
    transport.open()
    return Hbase.Client(protocol)

def close_trans(transport):
    transport.close()

def insert_data(client):
    table_name = 'emp2'
    row = 'row-key1'
    mutations = [Mutation(column='personal:name', value='raju')]
    mutations.append(Mutation(column='personal:city', value='hyderabad'))
    mutations.append(Mutation(column='professional:designation', value='manager'))
    mutations.append(Mutation(column='professional:salary', value='50000'))
    client.mutateRow(table_name, row, mutations)
    print("数据插入{}成功".format(table_name))



if __name__ == '__main__':
    socket = TSocket.TSocket('master', 9090)
    transport = TBufferedTransport(socket)
    client = create_client(transport)
    insert_data(client)
    close_trans(transport)